import soundfile as sf

from transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor
from qwen_omni_utils import process_mm_info

# default: Load the model on the available device(s)
# model = Qwen2_5OmniForConditionalGeneration.from_pretrained("Qwen/Qwen2.5-Omni-7B", torch_dtype="auto", device_map="auto")
# print(model)
# 我们建议启用 flash_attention_2 以获取更快的推理速度以及更低的显存占用.
# Set the checkpoint save path
ckpt_save_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/ckpt/qwen2_5_omini_7b/"

# Load the model
model = Qwen2_5OmniForConditionalGeneration.from_pretrained(
    "Qwen/Qwen2.5-Omni-7B",  # Pretrained model
    torch_dtype="auto",      # Automatically select dtype (float32, float16, etc.)
    device_map="auto",       # Automatically map the model to available devices
    # attn_implementation="flash_attention_2",  # Use flash attention implementation
    cache_dir=ckpt_save_path # Set the checkpoint save path
)
print(model)

processor = Qwen2_5OmniProcessor.from_pretrained("Qwen/Qwen2.5-Omni-7B")
audio_path = "/apdcephfs_qy3/share_976139/users/xuelonggeng/data/asr_test/aishell_test/ft_local/speech_asr_aishell_testsets/wav/test/S0764/BAC009S0764W0130.wav"
conversation = [
    {
        "role": "system",
        "content": [
            {"type": "text", "text": "You are Qwen, a virtual human developed by the Qwen Team, Alibaba Group, capable of perceiving auditory and visual inputs, as well as generating text and speech."}
        ],
    },
    {
        "role": "user",
        "content": [
            # {"type": "video", "video": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-Omni/draw.mp4"},
            {"type": "audio", "audio": audio_path},
        ],
    },
]


# set use audio in video
USE_AUDIO_IN_VIDEO = True

# Preparation for inference
text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
audios, images, videos = process_mm_info(conversation, use_audio_in_video=USE_AUDIO_IN_VIDEO)
inputs = processor(text=text, audio=audios, images=images, videos=videos, return_tensors="pt", padding=True, use_audio_in_video=USE_AUDIO_IN_VIDEO)
inputs = inputs.to(model.device).to(model.dtype)

# Inference: Generation of the output text and audio
text_ids, audio = model.generate(**inputs, use_audio_in_video=USE_AUDIO_IN_VIDEO)

text = processor.batch_decode(text_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
print(text)
sf.write(
    "output.wav",
    audio.reshape(-1).detach().cpu().numpy(),
    samplerate=24000,
)